package com.example.work12;

import android.Manifest;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.example.work12.adapter.MainAdapter;
import com.example.work12.fragment.MusicFragment;
import com.example.work12.fragment.NullOneFragment;
import com.example.work12.fragment.NullThreeFragment;
import com.example.work12.fragment.NullTwoFragment;
import com.example.work12.recevier.MyRecevier;
import com.example.work12.service.MyService;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager mVp;
    private TabLayout mTab;
    private List<Fragment> fragmentList = new ArrayList<>();
    public MyService service1;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1){
                service1.shang();
            }else if(msg.what == 2){
                service1.next();
            }else if(msg.what == 3){
                service1.stop();
            }else {
                service1.start();
            }
        }
    };
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MyService.MyBinder binder = (MyService.MyBinder) service;
            service1 =  binder.getInstance();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mVp = (ViewPager) findViewById(R.id.vp);
        mTab = (TabLayout) findViewById(R.id.tab);

        MusicFragment musicFragment = new MusicFragment();
        NullOneFragment nullOneFragment = new NullOneFragment();
        NullTwoFragment nullTwoFragment = new NullTwoFragment();
        NullThreeFragment nullThreeFragment = new NullThreeFragment();

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
        }

        Intent intent = new Intent(this, MyService.class);
        startService(intent);
        bindService(intent,connection, Service.BIND_AUTO_CREATE);

        MyRecevier myRecevier = new MyRecevier(handler);
        IntentFilter filter = new IntentFilter();
        filter.addAction("shang");
        filter.addAction("next");
        filter.addAction("stop");
        filter.addAction("start");
        registerReceiver(myRecevier,filter);

        fragmentList.add(musicFragment);
        fragmentList.add(nullOneFragment);
        fragmentList.add(nullTwoFragment);
        fragmentList.add(nullThreeFragment);

        List<String> stringList = new ArrayList<>();
        stringList.add("音乐");
        stringList.add("空1");
        stringList.add("空2");
        stringList.add("空3");

        MainAdapter mainAdapter = new MainAdapter(getSupportFragmentManager(), fragmentList, stringList);
        mVp.setAdapter(mainAdapter);
        mTab.setupWithViewPager(mVp);

        mTab.getTabAt(0).setIcon(R.drawable.selecet);
        mTab.getTabAt(1).setIcon(R.drawable.selecet);
        mTab.getTabAt(2).setIcon(R.drawable.selecet);
        mTab.getTabAt(3).setIcon(R.drawable.selecet);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }

    public void click(View view) {
         switch(view.getId()){
             case R.id.stop:
                 service1.stop();
                 break;
             case R.id.shang:
                 service1.shang();
                 break;
             case R.id.next:
                 service1.next();
                 break;
             case R.id.start:
                 service1.start();
                 break;
             default:

                 break;
         }
    }
}
